﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;
using System.Text;

public class ShopItemUnit : MonoBehaviour {
    public UISprite itemImage;
	public UISprite skillItemImage;
	public UISprite charItemImage;
	public UILabel itemName;
	public UILabel itemDescText;
	public UISprite itemCostTypeImage;
    public UISprite[] itemCostTextImages;
	public UISprite saleRibon;
	public UILabel saleText;
	public UISprite giftIcon;
	public UISprite discountIcon;

	[HideInInspector]
    public int ItemId;

	public GameObject buyButton;

    public AddShopItemUnits manager;

	bool isAlreadyHadSkill = false;
	bool isAlreadyHadCharacter = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnClickedBuyButton()
    {
        CShopItem item = CShopItemDataManager.Instance.GetItemById(ItemId);
		Debug.LogError("OnClick : " + item.price_type);

		if( item.category == "skill" && isAlreadyHadSkill )
			return;

		if( item.category == "character" && isAlreadyHadCharacter )
			return;

        if (item.price_type == "diamond")
        {
            if (MyInfo.Instance.Diamond < item.price)
            {
                MessagePopup.window.ShowPopup(MESSAGEPOPUPTYPE.NotEnoughCash, null, PopupCallback);
				return;
            }
        }
        else if (item.price_type == "gold")
        {
            if (MyInfo.Instance.Money < item.price)
            {
                MessagePopup.window.ShowPopup(MESSAGEPOPUPTYPE.NotEnoughGold, null, PopupCallback);
				return;
            }
        }
		
		string[] param = new string[1];
		param[0] = CStringTableManager.Instance.GetItemByName(item.title_string);
        MessagePopup.window.ShowPopup(MESSAGEPOPUPTYPE.BUYITEM, param, PopupCallback);
    }

    void ReturnCallback(Hashtable returnData)
    {
        LoadingWindow.window.HideLoading();
		switch((RestAPIProtocol)returnData["protocol"])
		{
		case RestAPIProtocol.BUYSHOPITEM:
	        if( (bool)returnData["isSuccess"] )
	        {
	            CShopItem item = CShopItemDataManager.Instance.GetItemById(ItemId);
	            string[] amount = item.contents.Split(',');
	            if (item.category == "cash")
	            {
	                MyInfo.Instance.Diamond += int.Parse(amount[1]);
	            }
	            else if (item.category == "gold")
	            {
	                MyInfo.Instance.Money += int.Parse(amount[1]);
	                MyInfo.Instance.Diamond -= item.price;
	            }
	            else if (item.category == "stamina")
	            {
	                MyInfo.Instance.Stamina += 50*(int.Parse(amount[1])/100);
	            }
	            else if (item.category == "item" )
	            {
	                if (item.price_type == "diamond")
	                    MyInfo.Instance.Diamond -= item.price;
	                else if (item.price_type == "gold")
	                    MyInfo.Instance.Money -= item.price;

					if( item.contents.Contains("inventory") )
						MyInfo.Instance.AdditionalInvenCount += 5;
	                if( item.contents.Contains("deck") )
	                {
						LoadingWindow.window.ShowShortLoading();
						RestAPI.singleton.RefreshMyDeckInfo(ReturnCallback);
	                }
	            }
				else if(item.category == "skill")
				{
                    if (item.price_type == "diamond")
                        MyInfo.Instance.Diamond -= item.price;
                    else if (item.price_type == "gold")
                        MyInfo.Instance.Money -= item.price;

                    //buyButton.SetActive(false);
					isAlreadyHadSkill = true;
					CShopItem shopItemInfo = CShopItemDataManager.Instance.GetItemById(ItemId);
					string[] split = shopItemInfo.contents.Split(',');
					CSkillData skillData = CSkillDataManager.Instance.FindSkillData(split[1]);
					MyInfo.Instance.MySkillList.Add(skillData.id);
                    manager.RefreshItems();
	//				collider.enabled = false;
				}
				else if(item.category == "character")
				{
                    if (item.price_type == "diamond")
                        MyInfo.Instance.Diamond -= item.price;
                    else if (item.price_type == "gold")
                        MyInfo.Instance.Money -= item.price;

                    //buyButton.SetActive(false);
					isAlreadyHadCharacter = true;
					MyInfo.Instance.MyCharacterList.Add(ItemId%100);
                    manager.RefreshItems();
                    LoadingWindow.window.ShowShortLoading();
                    RestAPI.singleton.SetCurrentCharacter(ReturnCallback, ItemId % 100);
				}
	//			if( ShopMainManager.singleton != null )
				//            	ShopMainManager.singleton.RefreshTopInfos();
				string[] param = new string[1];
				param[0] = CStringTableManager.Instance.GetItemByName(item.title_string);
				MessagePopup.window.ShowPopup(MESSAGEPOPUPTYPE.SuccessBuyShopItem, param, null);
	        }
			break;
		case RestAPIProtocol.REFRESHMYDECKINFO:
			break;
        case RestAPIProtocol.SETCURRENTCHARACTER:
            MyInfo.Instance.pawnId = ItemId % 100;
            break;
		}
    }
	
	void PopupCallback(MESSAGEPOPUPTYPE type, bool isYes)
	{
		if( type == MESSAGEPOPUPTYPE.BUYITEM )
		{
			LoadingWindow.window.ShowShortLoading();
			RestAPI.singleton.BuyShopItem(ReturnCallback, ItemId);
		}
		else if( type == MESSAGEPOPUPTYPE.NotEnoughGold )
		{
			if( ShopMainManager.singleton != null )
			{
				ShopMainManager.singleton.SelectTab(SHOPTYPE.GOLD);
			}
		}
		else if( type == MESSAGEPOPUPTYPE.NotEnoughCash )
		{
			if( ShopMainManager.singleton != null )
			{
				ShopMainManager.singleton.SelectTab(SHOPTYPE.CASH);
			}
		}
	}
}
